<?php

namespace App;

use App\Models\AdminUser;
use App\Models\Department;
use App\Models\TransverseLog;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Transverse extends BaseModel
{
    protected $casts = [
        'participants' => 'json'
    ];

    public function logs()
    {
        return $this->hasMany(TransverseLog::class, 'transverse_id');
    }

    public function user()
    {
        return $this->belongsTo(AdminUser::class, 'user_id');
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    public function creator()
    {
        return $this->belongsTo(AdminUser::class, 'creator_id');
    }

    public function bills(): HasMany
    {
        return $this->hasMany(TransverseBill::class, 'transverse_id');
    }

    public function writeLog($user, $msg, $remark = null)
    {
        $this->logs()->create([
            'title'     => $msg,
            'user_id'   => $user['id'],
            'user_name' => $user['name'],
            'remark'    => $remark,
        ]);
    }

}
